package com.iims.flowable.flow.vo.request;

import cn.hutool.core.lang.Validator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.iims.common.core.domain.entity.SysUser;
import com.iims.common.core.page.PageDomain;
import com.iims.common.utils.SecurityUtils;
import com.iims.common.utils.StringUtils;
import com.iims.flowable.common.constant.FlowableConstant;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;

import java.util.Date;

/**
 * 任务已办/待办/待阅搜索视图
 *
 * @author fanpei
 * @date 2021/6/15 10:45
 */
@Data
public class FlowTaskQueryVo extends PageDomain implements IBaseQueryVo<TaskInfoQuery> {

    @ApiModelProperty("任务名称")
    private String name;

    @ApiModelProperty("创建开始时间:format = \"yyyy-MM-dd\"")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date beginTime;

    @ApiModelProperty("创建结束时间:format = \"yyyy-MM-dd\"")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date endTime;

    @ApiModelProperty(hidden = true)
    private ListQUeryTaskType taskType;


    @Override
    public Query genSearchQuery(TaskInfoQuery query) {
        if (StringUtils.isNotEmpty(name)) {
            query = query.taskNameLikeIgnoreCase(name);
        }

        if (Validator.isNotNull(beginTime) && Validator.isNotNull(endTime)) {
            query = query.taskCreatedAfter(beginTime).taskCreatedBefore(endTime);
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        switch (taskType) {
            case ToRead:
                TaskQuery taskQueryToRead = (TaskQuery) query;
                taskQueryToRead.taskCategory(FlowableConstant.CATEGORY_TO_READ);
                if (!user.isAdmin()) {
                    taskQueryToRead = taskQueryToRead
                            .or()
                            .taskAssignee(user.getUser_code())
                            .taskOwner(user.getUser_code())
                            .endOr();
                }
                query = taskQueryToRead;
                break;
            case Finished:
                HistoricTaskInstanceQuery htQUery = (HistoricTaskInstanceQuery) query;
                htQUery = htQUery.finished();
                if (!user.isAdmin()) {
                    htQUery = htQUery
                            .or()
                            .taskAssignee(user.getUser_code())
                            .taskOwner(user.getUser_code())
                            .endOr();
                }
                query = htQUery;
                break;
            case ToDo:
                TaskQuery taskQueryToDo = (TaskQuery) query;
                taskQueryToDo.taskCategory(FlowableConstant.CATEGORY_TODO);
                if (!user.isAdmin()) {
                    taskQueryToDo = taskQueryToDo
                            .or()
                            .taskCandidateOrAssigned(user.getUser_code())
                            .taskOwner(user.getUser_code())
                            .endOr();
                }
                query = taskQueryToDo;
                break;
            default:
                break;
        }
        return query;
    }

    @Override
    public PageDomain selectPageDomain() {
        return this;
    }

    /**
     * 任务类型
     */
    public static enum ListQUeryTaskType {
        Finished,
        ToDo,
        ToRead
    }
}
